package com.wjk.kylin.modules.system.dict.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wjk.kylin.core.common.aspect.Log;
import com.wjk.kylin.core.common.controller.BaseController;
import com.wjk.kylin.core.common.data.RequestData;
import com.wjk.kylin.core.common.data.ResponseData;
import com.wjk.kylin.core.common.valid.insert;
import com.wjk.kylin.core.common.valid.update;
import com.wjk.kylin.core.utils.PageUtils;
import com.wjk.kylin.modules.system.dict.entity.DictValue;
import com.wjk.kylin.modules.system.dict.service.IDictValueService;
import com.wjk.kylin.modules.system.vo.ListSelectVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


import java.util.List;

import static com.wjk.kylin.core.constant.CommonConstant.BusinessType.*;

/**
 * <p>
 *  字典数据表 前端控制器
 * </p>
 *
 * @author wangjkui
 * @since 2020-06-07
 */
@Api("字典数据")
@RestController
@RequestMapping("/sys/dictValue")
public class DictValueController extends BaseController {
    @Autowired
    private IDictValueService dictValueService;

    @PreAuthorize("hasAuthority('system:dict:list')")
    @ApiOperation("列表")
    @Log(title = "字典数据-列表",businessType = LIST,isSaveResponseData = false)
    @GetMapping("/list")
    public ResponseData list(DictValue dictValue) {
        IPage<DictValue> page = dictValueService.findPage(new RequestData<DictValue>(), dictValue);
        return ResponseData.success().data("page", new PageUtils(page));
    }

    @PreAuthorize("hasAuthority('system:dict:query')")
    @ApiOperation("详情")
    @Log(title = "字典数据-详情",businessType = GET)
    @GetMapping("/{dictValueId}")
    public ResponseData get(@PathVariable String dictValueId) {
        DictValue dictValue = dictValueService.getById(dictValueId);
        return ResponseData.success().data("dictValue", dictValue);
    }

    @PreAuthorize("hasAuthority('system:dict:add')")
    @ApiOperation("新增")
    @Log(title = "字典数据-新增",businessType = INSERT)
    @PostMapping
    public ResponseData save(@Validated(insert.class) @RequestBody DictValue dictValue) {
        boolean flag = dictValueService.save(dictValue);
        if (flag) {
            return ResponseData.success();
        } else {
            return ResponseData.error("新增失败");
        }
    }

    @PreAuthorize("hasAuthority('system:dict:edit')")
    @ApiOperation("编辑")
    @Log(title = "字典数据-编辑",businessType = UPDATE)
    @PutMapping
    public ResponseData update(@Validated(update.class) @RequestBody DictValue dictValue) {
        boolean flag = dictValueService.update(dictValue);
        if (flag) {
            return ResponseData.success();
        } else {
            return ResponseData.error("编辑失败");
        }
    }

    @PreAuthorize("hasAuthority('system:dict:delete')")
    @ApiOperation("删除")
    @Log(title = "字典数据-删除",businessType = DELETE)
    @DeleteMapping("/{dictValueIds}")
    public ResponseData delete(@PathVariable String[] dictValueIds) {

        boolean flag = dictValueService.deleteByIds(dictValueIds);
        if (flag) {
            return ResponseData.success();
        } else {
            return ResponseData.error("删除失败");
        }
    }


    @ApiOperation("字典数据-字典类型查询字典数据")
    @GetMapping("/type/{dictType}")
    public ResponseData dictType(@PathVariable String dictType) {
        List<ListSelectVO> list = dictValueService.getDictValueByType(dictType);

        return ResponseData.success().data("list",list);
    }

}
